#coding=utf-8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import requests
import time
import sys

url = sys.argv[1]

service = Service('/usr/bin/chromedriver')
chrome_options=Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(service=service,options=chrome_options)

def down_mp4(url):
    try:
        driver.get(url)
        time.sleep(9)
        d_url = driver.find_element(By.XPATH,"//xg-video-container[@class='xg-video-container']//video//source").get_attribute("src")

        req = requests.get(d_url,stream=True)
        d_name = url.split('/')[-1]
        with open('%s.mp4'%d_name,'wb') as ali:
            for chunk in req.iter_content(chunk_size=1024):
                if chunk:
                    ali.write(chunk)
    except:
        print(url)


down_mp4(url)
time.sleep(5)

driver.quit()
